package com.example.commonlib;

import android.os.Bundle;
import android.view.KeyEvent;

import com.example.commonlib.databinding.ActivityMainBinding;
import com.hyk.commonLib.common.activity.BaseActivity;
import com.hyk.commonLib.common.dialogView.BottomTimeSelectorDialog;
import com.hyk.commonLib.common.utils.AppUtils;
import com.hyk.commonLib.common.utils.QrCodeUtils;
import com.hyk.commonLib.common.utils.ToastUtil;
import com.hyk.commonLib.common.utils.eventHandler.ActivityEventHandler;
import com.hyk.commonLib.common.utils.rx.EmptyObserver;
import com.hyk.commonLib.common.utils.viewModel.BaseObservableModel;
import com.lxj.xpopup.XPopup;

import org.jetbrains.annotations.NotNull;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding.setEventHandler(new EventHandler(this));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    public static final class MainActivityModel extends BaseObservableModel {
    }

    public static final class EventHandler extends ActivityEventHandler<MainActivity, MainActivityModel> {

        public EventHandler(@NotNull MainActivity activity) {
            super(activity, null);
        }

        @CheckContainer
        public void onErrorClicked() {
            QrCodeUtils.startScanQr(getContainer())
                    .subscribe(new EmptyObserver<String>() {
                        @Override
                        public void onNext(@NotNull String s) {
                            ToastUtil.getSingleton().showSuccess(s);
                        }

                        @Override
                        public void onError(@NotNull Throwable e) {
                            super.onError(e);
                            ToastUtil.getSingleton().showError(e.getMessage());
                        }
                    });
        }

        //        @CheckContainer
        public void onSuccessClicked() {

            BottomTimeSelectorDialog bottomCalendarDialog = new BottomTimeSelectorDialog(getActivity());

//            bottomCalendarDialog.getDataBinding().getModel().setSelectDate("2021-02-03").setOnDateSelectListener(new BottomCalendarDialog.OnDateSelectListener() {
//                @Override
//                public void onSelect(String date, int year, int monthOfYear, int dayOfMonth) {
//                    System.out.println(date);
//                    System.out.println(year);
//                    System.out.println(monthOfYear);
//                    System.out.println(dayOfMonth);
//                }
//            });

            new XPopup.Builder(getActivity())
                    .asCustom(bottomCalendarDialog)
                    .show();

            AppUtils.runOnUIDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.getSingleton().showSuccess("setter");
                    bottomCalendarDialog.getDataBinding().getModel()
                            .setSelectTime(12, 23, 45)
                            .setSelectType(BottomTimeSelectorDialog.TimeSelectType.HOUR_MINUTE)
                            .setOnTimeSelectListener(new BottomTimeSelectorDialog.OnTimeSelectListener() {
                                @Override
                                public void onSelect(String time, int hour, int minute, int second) {
                                    System.out.println(time);
                                    System.out.println(hour);
                                    System.out.println(minute);
                                    System.out.println(second);
                                }
                            });
                }
            }, 3000);

        }
    }

}
